''' Mission 5 Line Sense Final program with changes ''' from botcore import * from time import sleep from random import randrange threshold = 2000 line_count = 0 def detect_line(n): val = ls.read(n) is_detected = val < threshold leds.ls_num(n, is_detected) return is_detected def scan_lines(): got_line = False n_sens = 0 while n_sens < 5: if detect_line(n_sens): got_line = True n_sens = n_sens + 1 return got_line def go_forward(speed): motors.run(LEFT, speed) motors.run(RIGHT, speed) def back_turn(speed, delay): motors.run(LEFT, -speed) motors.run(RIGHT, -speed) sleep(1.5) motors.run(LEFT, speed) motors.run(RIGHT, -speed) sleep(delay) # -- Main Program -- motors.enable(True) while True: if buttons.was_pressed(0): break while True: hit = scan_lines() if hit: back_turn(20, 1.3) line_count = line_count + 1 if line_count == 256: line_count = 0 leds.user(line_count) else: speed = randrange(30, 50) go_forward(speed)